import numpy as np
import pandas as pd


train_data = pd.read_csv("train_data.csv")
test_data = pd.read_csv("test_data.csv")

X= train_data[['x1', 'x2']].values
labels = train_data['y'].values

test_X = test_data[['x1', 'x2']].values
test_labels = test_data['y'].values

#print(X[0])
#print(type(labels))

epochs = 1000
learning_rate = 0.001

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def logistic_regression(x, y):
    w = np.array([1, 1], dtype=np.float)
    b = 0.0
    #print(type(w[0]))

    for epoch in range(epochs):
        for i in range(len(x)):
            y_hat = sigmoid(np.dot(w , x[i]) + b)
            delt = learning_rate * (y[i] - y_hat)

            #print(type(delt))
            #print(type(w))
            #tem = w * delt
            w += w * delt
            b += delt
    return w, b

def predict(x, w, b):
    y_hat = sigmoid(np.dot(w, x) + b)
    if y_hat >= 0.5:
        return 1
    return 0

w, b = logistic_regression(X, labels)

print(w)
print(b)

acturly_cnt = 0

for i in range(len(test_labels)):
    pred = predict(test_X[i], w, b)

    if (pred == test_labels[i]):
        acturly_cnt += 1


print("the ration is: %f" % (acturly_cnt / len(test_labels)))
